package com.dap.core.context;

import java.util.HashMap;
import java.util.Map;


public class DapThreadContext {
    private static final ThreadLocal<Map<String, Object>> CTX_HOLDER =
            new ThreadLocal<Map<String, Object>>() {
                @Override
                protected Map<String, Object> initialValue() {
                    return new HashMap<>();
                };
            };

    public static final void putContext(String key, Object value) {
        CTX_HOLDER.get().put(key, value);
    }


    @SuppressWarnings("unchecked")
    public static final <T> T getContext(String key) {
        return (T) CTX_HOLDER.get().get(key);
    }


    public static final Map<String, Object> getContext() {
        return CTX_HOLDER.get();
    }


    public static final void remove(String key) {
        CTX_HOLDER.get().remove(key);
    }


    public static final boolean contains(String key) {
        return CTX_HOLDER.get().containsKey(key);
    }


    public static final void clean() {
        CTX_HOLDER.get().clear();
    }


    public static final void init() {
        CTX_HOLDER.get().clear();
    }
}
